﻿ ///// <reference path='lib.d.ts' />

module Common {
    export interface IEventHandler<T> {
        on(handler: { (data?: T): void });
        off(handler: { (data?: T): void });
        trigger(data?: T);
    }

    export class EventHandler<T> implements IEventHandler<T> {
        private _handlers: { (data?: T): void; }[] = [];

        public on(handler: { (data?: T): void }) {
            this._handlers.push(handler);
        }

        public off(handler: { (data?: T): void }) {
            this._handlers.splice(jQuery.inArray(handler, this._handlers), 1);
        }

        public trigger(data?: T) {
            if (this._handlers) {
                this._handlers.slice(0).forEach(h => h(data));
            }
        }
    }

    /**
     * 前端错误输出
     */
    export function ShowError(err) {
        if (err.responseJSON)
        {
            var msg = err.responseJSON.ExceptionMessage;
            if (!msg) msg = err.responseJSON.Message;
            $.messager.alert('错误提示', msg , "error");
        }
        else
            $.messager.alert('错误提示', "状态提示 : " + err.statusText, "error");
    }

    export interface Action {
        (): void;
    }

    export interface Func<T, TResult> {
        (item: T): TResult;
    }

    /**
     * 事件参数
     */
    export class EventArgs {
        constructor(private act: Action) {
            this.callback = act;
        }
        /**
         * 事件回调
         */
        public callback: Action
    }

    // #region  KeyValuePair
    /**
     * 可设置或检索的键/值对
     */
    export class KeyValuePair<TKey, TValue>
    {
        public key: TKey;
        public value: TValue;
    }
//#endregion
}


// #region  String.format
interface String {
    /**
     * 字符串格式化
     */
    format(...replacements: string[]): string;
}

if (!String.prototype.format) {
    String.prototype.format = function () {
        var args = arguments;
        return this.replace(/{(\d+)}/g, function (match, number) {
            return typeof args[number] != 'undefined'
                ? args[number]
                : match
                ;
        });
    };
}
//#endregion


// #region  扩展easyui表单的验证 
$.extend($.fn.validatebox.defaults.rules, {
    //验证汉字  
    CHS: {
        validator: function (value) {
            return /^[\u0391-\uFFE5]+$/.test(value);
        },
        message: 'The input Chinese characters only.'
    },
    //移动手机号码验证  
    Mobile: {//value值为文本框中的值  
        validator: function (value) {
            var reg = /^1[3|4|5|8|9]\d{9}$/;
            return reg.test(value);
        },
        message: 'Please enter your mobile phone number correct.'
    },
    //国内邮编验证  
    ZipCode: {
        validator: function (value) {
            var reg = /^[0-9]\d{5}$/;
            return reg.test(value);
        },
        message: 'The zip code must be 6 digits and 0 began.'
    },
    //数字  
    Number: {
        validator: function (value) {
            var reg = /^[0-9]*$/;
            return reg.test(value);
        },
        message: 'Please input number.'
    },
    MinLength: {
        validator: function (value, param) {   //value 为需要校验的输入框的值 , param为使用此规则时存入的参数
            return value.length >= param[0];
        },
        message: '请输入最小{0}位字符.'
    },
    MaxLength: {
        validator: function (value, param) {
            return param[0] >= value.length;
        },
        message: '请输入最大{0}位字符.'
    },
    Length: {
        validator: function (value, param) {
            return value.length >= param[0] && param[1] >= value.length;
        },
        message: '请输入{0}-{1}位字符.'
    },
    Equals: {
        validator: function (value, param) {
            return value == $(param[0]).val();
        },
        message: '字段不相同.'
    },
    Web: {
        validator: function (value) {
            return /^(http[s]{0,1}|ftp):\/\//i.test($.trim(value));
        },
        message: '网址格式错误.'
    },
    Date: {
        validator: function (value) {
            return /^[0-9]{4}[-][0-9]{2}[-][0-9]{2}$/i.test($.trim(value));
        },
        message: '曰期格式错误,如2012-09-11.'
    },
    Email: {
        validator: function (value) {
            return /^[a-zA-Z0-9_+.-]+\@([a-zA-Z0-9-]+\.)+[a-zA-Z0-9]{2,4}$/i.test($.trim(value));
        },
        message: '电子邮箱格式错误.'
    }
})
//#endregion